
/*------------------------------------------------------------------------------------------------------------------
Includes
*/
#include "stm32l4xx_hal.h"

#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"

#include "xBSP_Config.h"
#include "xBSP_Gpio.h"
#include "xBSP_Pwm.h"
#include "xBSP_Spi.h"

#include "xSYS_KeyScan.h"
#include "xSYS_ST7789.h"

#include "xAPP_Config.h"
#include "xAPP_KeyDeal.h"

#include "lvgl.h"
#include "xGUI_MainTask.h"

/*------------------------------------------------------------------------------------------------------------------
Macros
*/
#define RTOS_TASK_NUM(x)   (sizeof(x)/sizeof(x[0]))

/*------------------------------------------------------------------------------------------------------------------
Variables
*/
TaskHandle_t Handle_StartTask = NULL;
TimerHandle_t ReloadTimer1_Handle;
static uint32_t timer200msID = 1;


/*------------------------------------------------------------------------------------------------------------------
Function
*/
static void SoftWareTimerCallback(TimerHandle_t xTimer);
static void xTask_Start(void *pvParameters);
void xAPP_IOScanTask(void *pvParameters);

RtosTaskManage_t TaskList[] =
{
  {(TaskFunction_t)xAPP_IOScanTask,"xAPP_IOScanTask",TASK_STACK_SIZE_IOScanTask,NULL,TASK_PRIORITY_IOScanTask,NULL},
  {(TaskFunction_t)xGUI_MainTask,"xGUI_MainTask",TASK_STACK_SIZE_GuiMainTask,NULL,TASK_PRIORITY_GuiMainTask,NULL}
};

int main(void)
{
  xBSP_Config();
    
  /*Create Start Task*/
  if(xTaskCreate(xTask_Start, "Task_Start", TASK_STACK_SIZE_StartTask, NULL, TASK_PRIORITY_StartTask, &Handle_StartTask) != pdPASS)
  {
    Error_Handler();
  }

  vTaskStartScheduler();	//开始系统调度

  while(1)
  {

  }
}

/*
***************************************************************************************************************
@ Brief  : Create a start task and to do something.

@ Param  : None

@ Return : NONE

@ Author : LYC

@  Date  : 2021 - 07 - 12
***************************************************************************************************************
*/
static void xTask_Start(void *pvParameters)
{
  int i;
  BaseType_t xReturn;

  xBSP_GPIO_Init();
  xSYS_KeyScan_RegisterCallback(xAPP_KEY_KeyEventCallback);

  xBSP_PWM_Init(1000 - 1, 80 - 1);

  xBSP_Spi_init();

  //任务列表创建  (sizeof(x)/sizeof(x[0]))  RTOS_TASK_NUM
  for(i=0;i<RTOS_TASK_NUM(TaskList);i++)
  {
    xReturn = xTaskCreate(TaskList[i].func, TaskList[i].name, TaskList[i].stackDepth, TaskList[i].userPara, TaskList[i].priority, TaskList[i].handle);
    if(xReturn != pdPASS)
    {
      Error_Handler();
    }
  }

  //创建软件周期定时器  pdTRUE:自动重载  pdFALSE:一次性定时器
  ReloadTimer1_Handle = xTimerCreate(NULL,pdMS_TO_TICKS(200),pdTRUE,(void*)&timer200msID,SoftWareTimerCallback); 
  if(ReloadTimer1_Handle == NULL)
  {
    Error_Handler();
  }

  xTimerStart(ReloadTimer1_Handle,pdMS_TO_TICKS(10));

  /*删除开始任务*/
  vTaskDelete(NULL);
}

/*
***************************************************************************************************************
@ Brief  : 软定时器

@ Param  : None

@ Return : None

@ Author : LYC 

@  Date  : 2020 - 04 - 03
***************************************************************************************************************
*/
static void SoftWareTimerCallback(TimerHandle_t xTimer)
{
  uint32_t *pTimerId;

  pTimerId = (uint32_t *)pvTimerGetTimerID(xTimer);

  if(*pTimerId == timer200msID)
  {
    if(ledBlinkStep == 0)
    {
      LED_R_TOGGLE();
    }
    else if(ledBlinkStep == 1)
    {
      LED_G_TOGGLE();
    }
    else
    {
      LED_B_TOGGLE();
    }
  }
}

/*
********************************************************************************************************************
@ Brief  : IO 扫描任务

@ Param  : None

@ Return : None

@ Author : LYC

@  Date  : 2020 - 04 - 03
********************************************************************************************************************
*/
void xAPP_IOScanTask(void *pvParameters)
{

  while(1)
  {
    /*按键扫描*/
    xSYS_KeyScan();
    vTaskDelay(10);
  }
}


/*
***************************************************************************************************************
@ Brief  : 钩子函数创建（空闲任务）  用于空闲喂狗，该线程长时间阻塞（出现异常）将复位

@ Param  : None

@ Return : None

@ Author : LYC

@  Date  : 2020 - 04 - 03
***************************************************************************************************************
*/
void vApplicationTickHook(void)
{
    if (s_lvgl_initialized)
    {
        lv_tick_inc(1);
    }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */


#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
